﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Routing;

namespace LevenBlog.Core.Install {
    public class InstallCheckHandle : UrlRoutingHandler {
        protected override void VerifyAndProcessRequest(IHttpHandler httpHandler, HttpContextBase httpContext) {
            httpContext.Response.ContentType = "application/json";
            httpContext.Response.Write("true");
        }

        public static void Init(IList<string> extends) {
            string url = "install/check/test{0}";
            InstallCheckHandle checkHandle = new InstallCheckHandle();
            foreach (var item in extends) {
                Route newRoute = new Route(String.Format(url, item), new InstallCheckRouteHandle());
                string name = String.Format("check_{0}", item);
                if (RouteTable.Routes[name] == null) {
                    RouteTable.Routes.Add(name, newRoute);
                }
            }
        }

        public static void UnLoad(IList<string> extends) {
            foreach (var item in extends) {
                RouteBase find = RouteTable.Routes[String.Format("check_{0}", item)];
                if (find != null) {
                    RouteTable.Routes.Remove(find);
                }
            }
        }
    }

    public class InstallCheckRouteHandle : IRouteHandler {
        #region IRouteHandler Members
        private static IHttpHandler installCheckHandle = new InstallCheckHandle();

        public IHttpHandler GetHttpHandler(RequestContext requestContext) {
            return installCheckHandle;
        }

        #endregion
    }
}
